package com.wanho.java157.SystemSetting.controller;

import com.wanho.java157.SystemSetting.po.Employee;
import com.wanho.java157.SystemSetting.po.Menu;
import com.wanho.java157.SystemSetting.po.Position;
import com.wanho.java157.SystemSetting.service.MenuService;
import com.wanho.java157.commons.util.vo.JSONResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.SessionAttributes;

import java.util.List;

@Controller
@RequestMapping("/menu")
@SessionAttributes({"employee","menuList","updateMenu","allParentMenu"})
public class MenuController {
    @Autowired
    private MenuService menuService;

    @RequestMapping("/check")
    public String menuCheck(Model model){
        Employee employee=(Employee)model.getAttribute("employee");
        List<Menu> menuList = menuService.selectMenuByEmployeeid(employee);
        model.addAttribute("menuList",menuList);

        return "view/systemSettings/menu/index";

    }

    @RequestMapping("/search")
    public String menuSearch(String name,Model model){
        List<Menu> menuList = menuService.selectMenuByAmbiguousName(name);
        model.addAttribute("menuList",menuList);
        return "view/systemSettings/menu/index";
    }
    @RequestMapping("/add")
    public String menuAdd(Model model){
        List<Menu> allParentMenu = menuService.selectAllParentMenu();
        model.addAttribute("allParentMenu",allParentMenu);
        return "view/systemSettings/menu/create";
    }

    @RequestMapping("/successCreate")
    public String menuCreat(Menu menu){
        System.out.println(menu);
        Boolean insertMenu = menuService.insertMenu(menu);
        if(insertMenu){
            return "redirect:/menu/check";
        }
        return "redirect:/menu/add";
    }

    @RequestMapping("/update")
    public String menuUpdate(String menuId,Model model){
        Menu updateMenu = menuService.selectMenuByMenuId(menuId);
        System.out.println(updateMenu);
        List<Menu> allParentMenu = menuService.selectAllParentMenu();
        model.addAttribute("allParentMenu",allParentMenu);
        model.addAttribute("updateMenu",updateMenu);
        return "view/systemSettings/menu/update";
    }

    @RequestMapping("/successupdate")
    public String menuSuccessUpdate(Menu menu){
        Menu parentMenu = menu.getParentMenu();
        if(parentMenu != null){
            Menu menu1 = menuService.selectMenuByMenuName(parentMenu.getMenuName());
            menu.setParentMenu(menu1);
        }
        menuService.updateMenuDetailById(menu);

        return "redirect:/menu/check";
    }

    @RequestMapping("/delete")
    @ResponseBody
    public JSONResult menuDelete(String menuId,Model model){
        System.out.println(menuId);
        Boolean delete = menuService.deleteById(menuId);
        JSONResult jsonResult = new JSONResult();
        if (delete){
            jsonResult.setFlag(1);
            jsonResult.setMsg("新增成功");
            return jsonResult;

        }
        jsonResult.setFlag(0);
        jsonResult.setMsg("新增失败");
        return jsonResult;

    }


}
